<?php
namespace Admin\Controller;
use Admin\Model\BaseinfoVersionModel;
use Base\Controller\CurdController;
use Library\Utils;

/**
 * Class AerainfoController
 * 地区控制器
 */
class AerainfoController extends CurdController
{
    public function __construct()
    {
        parent::__construct(true, true);

    }
    /**
     * 获取所有数据并注入模板
     * */
    protected function getAll()
    {
        if($_REQUEST['keyword']){
            $map['aerainfo_name'] = array('like','%'.$_REQUEST['keyword'].'%');
            $map['aerainfo_name_en']  = array('like', '%'.$_REQUEST['keyword'].'%');
            $map['aerainfo_shortname_en']  = array('like', '%'.$_REQUEST['keyword'].'%');
            $map['_logic'] = 'or';
            $where['_complex'] = $map;
        }
        if(!I('get.del')){
            $where['status']=1;
        }else{
            $where['status']=0;
        }
        if($_REQUEST['selects']){
            $limit=$_REQUEST['selects'];
        }else{
            $limit=20;
        }
        $data = $this->model->pageToView($limit, true,$where);
        $this->assign("page", $data['page']);
        $this->assign("list", $data['list']);

    }
    /**
     * 删除
     * @param string $id 要删除的id，多个以,分割
     */
    public function del($id)
    {
        $arr = explode(',', $id);
        $i = 0;
        foreach ($arr as $k => $v) {
            if (!$v) {
                continue;
            }
            $BaseinfoVersionModel=new BaseinfoVersionModel();
            $controller = hump2underline(CONTROLLER_NAME);
            $infos=$BaseinfoVersionModel->where(array('name'=>$controller))->find();
            $data['version']=$infos['version'];
            $data['status']=0;
            $this->model->where('AeraInfo_ID=' . $v)->save($data);
            $i++;
        }
        $this->success('删除' . $i . '条数据', $this->urlConfig['del_success']);
    }
    /**
     * 恢复删除
     * @param string $id 要恢复的id，多个以,分割
     */
    public function recover($id){
        $arr = explode(',', $id);
        $i = 0;
        foreach ($arr as $k => $v) {
            if (!$v) {
                continue;
            }
            $BaseinfoVersionModel=new BaseinfoVersionModel();
            $controller = hump2underline(CONTROLLER_NAME);
            $infos=$BaseinfoVersionModel->where(array('name'=>$controller))->find();
            $data['version']=$infos['version'];
            $data['status']=1;
            $this->model->where('AeraInfo_ID=' . $v)->save($data);
            $i++;
        }
        $this->success('恢复' . $i . '条数据', $this->urlConfig['del_success']);
    }
}